package com.ugoodtech.mdcc.core.service.impl;

import com.ugoodtech.mdcc.core.model.entity.CustomerRegularReport;
import com.ugoodtech.mdcc.core.model.entity.CustomerRegularReportExample;
import com.ugoodtech.mdcc.core.model.entity.StationColorReport;
import com.ugoodtech.mdcc.core.model.entity.StationColorReportExample;
import com.ugoodtech.mdcc.core.model.entity.StationColorReportExample.Criteria;
import com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReport;
import com.ugoodtech.mdcc.core.model.entity.StationRegularServiceReportExample;
import com.ugoodtech.mdcc.core.model.entity.StationReport;
import com.ugoodtech.mdcc.core.model.entity.StationReportExample;
import com.ugoodtech.mdcc.core.model.entity.Stations;
import com.ugoodtech.mdcc.core.model.entity.StationsExample;
import com.ugoodtech.mdcc.core.model.mapper.CustomerRegularReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationColorReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationRegularServiceReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationReportMapper;
import com.ugoodtech.mdcc.core.model.mapper.StationsMapper;
import com.ugoodtech.mdcc.core.service.MybatisStationReportService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Xiangcheng Li
 *
 * @author Xiangcheng Li
 */

@Service
@Slf4j
public class MybatisStationReportServiceImpl implements MybatisStationReportService {

    @Autowired
    private StationColorReportMapper stationColorReportMapper;

    @Autowired
    private StationReportMapper stationReportMapper;

    @Autowired
    private StationsMapper stationsMapper;

    @Autowired
    private StationRegularServiceReportMapper stationRegularServiceReportMapper;

    @Autowired
    private CustomerRegularReportMapper customerRegularReportMapper;

    public List<StationColorReport> listColorReport(List<Long> userIdList){


        // 查找维修站
        StationsExample stationsExample = new StationsExample();
        StationsExample.Criteria stationsExampleCriteria = stationsExample.createCriteria();
        stationsExampleCriteria.andReportFinishedIn(Arrays.asList(1, 2, 3));

        List<Stations> stations = stationsMapper.selectByExample(stationsExample);
        if (CollectionUtils.isEmpty(stations)) {
            return new ArrayList(0);
        }
        List<Long> stationIdList =
            stations.stream().map(Stations::getId).distinct().collect(Collectors.toList());

        // 筛选维修站报告
        StationReportExample stationReportExample = new StationReportExample();
        StationReportExample.Criteria stationReportExampleCriteria = stationReportExample.createCriteria();
        stationReportExampleCriteria.andStationIdIn(stationIdList.stream().distinct().collect(
            Collectors.toList()));
        if (!CollectionUtils.isEmpty(userIdList)) {
            stationReportExampleCriteria.andUserIdIn(userIdList);
        }
        List<StationReport> stationReportList = stationReportMapper.selectByExample(
            stationReportExample);
        if (CollectionUtils.isEmpty(stationReportList)) {
            return new ArrayList(0);
        }
        List<Long> stationReportIdList = stationReportList.stream().map(StationReport::getId)
            .collect(Collectors.toList());

        StationColorReportExample stationColorReportExample = new StationColorReportExample();
        Criteria criteria = stationColorReportExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andEnabledEqualTo(true);
        criteria.andReportIdIn(stationReportIdList);
        return stationColorReportMapper.selectByExample(stationColorReportExample);

    }

    public List<StationColorReport> listColorReport(String payService,String sentTime,String serviceTime) {
        StationColorReportExample stationColorReportExample = new StationColorReportExample();
        Criteria criteria = stationColorReportExample.createCriteria();
        if (StringUtils.isNotEmpty(payService)) {
            criteria.andAnswerIdInSet(payService);
        }
        if (StringUtils.isNotEmpty(sentTime)) {
            criteria.andAnswerIdInSet(sentTime);
        }
        if (StringUtils.isNotEmpty(serviceTime)) {
            criteria.andAnswerIdInSet(serviceTime);
        }
        return stationColorReportMapper.selectByExample(stationColorReportExample);
    }


    public List<StationReport> listReport(List<Long> reportIdList){
        if (CollectionUtils.isEmpty(reportIdList)) {
            return new ArrayList<>(0);
        }
        StationReportExample stationReportExample = new StationReportExample();
        StationReportExample.Criteria stationReportExampleCriteria = stationReportExample.createCriteria();
        stationReportExampleCriteria.andIdIn(reportIdList.stream().distinct().collect(Collectors.toList()));
        return stationReportMapper.selectByExample(stationReportExample);
    }


    public List<CustomerRegularReport> listCustomerRegularReport(Integer type) {


        CustomerRegularReportExample customerRegularReportExample = new CustomerRegularReportExample();
        CustomerRegularReportExample.Criteria criteria = customerRegularReportExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andEnabledEqualTo(true);
        if (Objects.nonNull(type)) {
            criteria.andTypeEqualTo(type);
        }

        return customerRegularReportMapper.selectByExample(customerRegularReportExample);

    }

    public List<Stations> listStations(List<Long> stationReportIdList) {
        StationReportExample stationReportExample = new StationReportExample();
        StationReportExample.Criteria stationReportExampleCriteria = stationReportExample.createCriteria();
        stationReportExampleCriteria.andIdIn(stationReportIdList.stream().distinct().collect(
            Collectors.toList()));
        List<com.ugoodtech.mdcc.core.model.entity.StationReport> stationReportList = stationReportMapper.selectByExample(
            stationReportExample);

        if (CollectionUtils.isEmpty(stationReportList)) {
            return new ArrayList<>(0);
        }
        List<Long> stationIdList = stationReportList.stream()
            .map(com.ugoodtech.mdcc.core.model.entity.StationReport::getStationId).collect(
                Collectors.toList());
        // 查找维修站
        StationsExample stationsExample = new StationsExample();
        StationsExample.Criteria stationsExampleCriteria = stationsExample.createCriteria();
        stationsExampleCriteria.andIdIn(stationIdList);
        return stationsMapper.selectByExample(stationsExample);

    }


    public Map<Long, List<StationRegularServiceReport>> mapIdToStationRegularServiceReport(List<Long> reportIdList){

        if (CollectionUtils.isEmpty(reportIdList)) {
            return new HashMap<>(0);
        }
        StationRegularServiceReportExample stationRegularServiceReportExample = new StationRegularServiceReportExample();
        StationRegularServiceReportExample.Criteria criteria = stationRegularServiceReportExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andEnabledEqualTo(true);
        criteria.andReportIdIn(reportIdList);

        List<StationRegularServiceReport> stationRegularServiceReportList = stationRegularServiceReportMapper.selectByExample(
            stationRegularServiceReportExample);
        return stationRegularServiceReportList.stream()
            .collect(Collectors.groupingBy(StationRegularServiceReport::getReportId));


    }
}
